//
//  StudysCoreMusicViewController.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/6/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "StudysCoreMusicViewController.h"
#import "ZJStudyCoureFooterView.h"
#import "SkinVideoViewController.h"
#import "ZJVideoControllerMeunView.h"
#import "ZJSCNoteModel.h"
#import "ZJSCSynopsisModel.h"
#import "ZJSCQusetionsSrectionModel.h"
#import "ZJStudyCoureSwithView.h"

#import "StudyCourseNoteViewController.h"
#import "StudyCourseQuestionsViewController.h"
#import "SendCommentView.h"
#import <IQKeyboardManager.h>
/** 笔记cell */
#import "ZJStudyCourseNoteTableViewCell.h"
#import "ZJStudyCourseNoteTableViewCellModel.h"
/** 评论cell */
#import "ZJStudyCourseQuestionsTableViewCell.h"
#import "ZJStudyCourseQuestionsTableViewCellModel.h"
/** 课程cell */
#import "ZJStudyCourseSynopsisTableViewCell.h"
#import "ZJStudyCourseSynopsisTableViewCellModel.h"

#define NOTECELL @"ZJStudyCourseNoteTableViewCell"
#define SYNOPSISSCECTIONVIEW @"ZJStudyCourseSynopsisTableViewCell.h"
#define COMMENTCELL @"ZJStudyCourseQuestionsTableViewCell.h"

@interface StudysCoreMusicViewController ()<UITableViewDelegate,UITableViewDataSource,ZJStudyCoureFooterViewDelegate,ZJVideoControllerMeunViewDelegate,ZJStudyCoureSwithViewDelegate,SendCommentViewDelegate,ZJSCNoteTableViewCellDelegate,ZJSCQusetionsSrectionViewCommentDelegate,ZJStudyCourseQuestionsTableViewCellDelegate,ZJStudyCourseNoteTableViewCellDelegate,UIGestureRecognizerDelegate>
{
    UIView *_commentBGView;/** 评论页面背景View */
    BOOL _isCourseComment;/**< 判断是否是课程评论、还是评论-评论 */
    BOOL _isCommentORNote;/**< YES:评论 NO:笔记 */
    ZJSCQusetionsSrectionModel *_commentSectionViewModel;/**< 记录评论SectionView的model */
    NSMutableDictionary *cellHeightDict;/**< 记录cell的高度 */
    /**< 课程简介是否展开  yes:展开  NO  收缩 */
    BOOL _synopsisIntroduce_isUnfold;
    /**< 教师简介是否展开  yes:展开  NO  收缩 */
    BOOL _teacherIntroduce_isUnfold;
}
/** 视频播放控制器 */
@property (nonatomic, strong)  SkinVideoViewController *videoPlayer;
/** 视频下部控制器 */
@property (nonatomic, strong) ZJVideoControllerMeunView *videoPlayerMeun;
/** 下面的控制器 */
@property (nonatomic, strong) ZJStudyCoureFooterView *footerView;
/** 数据 */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** tableview */
@property (nonatomic, strong) UITableView *tableView;
/** 选择VIew */
@property (nonatomic, strong) ZJStudyCoureSwithView *swithView ;
/** tableview__HeaderView */
@property (nonatomic, strong) UIView *headerView;

/** 评论模块 */
@property (nonatomic, strong) SendCommentView *sendCommentView;

@end

@implementation StudysCoreMusicViewController
- (void)viewDidDisappear:(BOOL)animated {
    if (!self.navigationController.viewControllers) {
        // 主动调用 cancel 方法销毁播放器
        [self.videoPlayer cancel];    // cancel方法中调用了cancelObserver
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    [super viewDidDisappear:animated];
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //关闭右滑pop
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = NO;
    //关闭右滑pop
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;

    cellHeightDict = [NSMutableDictionary dictionary];
    [self networking_introduce];
    [self setupUI];
    self.navigationController.navigationBar.hidden = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(currentTimeAndTotalTime:) name:ZJVideoControllerMeunViewTime object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];/**< 键盘将要出现的通知 */
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDismiss:) name:UIKeyboardWillHideNotification object:nil];/**< 键盘将要消失的通知 */
    //接受简介cell刷新
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(IntroduceCell_reLoadTableViewCell:) name:INTRODUCECELL_RELOAD object:nil];
    
}
- (void)setupUI{
    [self.view addSubview:self.footerView];
    [self initVideoPlayer];
    [self.view addSubview:self.videoPlayerMeun];
    [self.view addSubview:self.swithView];
    [self.view addSubview:self.tableView];
    

    
    _commentBGView = [[UIView alloc] initWithFrame:self.view.bounds];
    _commentBGView.backgroundColor = [UIColor blackColor];
    _commentBGView.alpha = 0.2;
    _commentBGView.hidden = YES;
    UITapGestureRecognizer *tapGester = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_tapGester)];
    [_commentBGView addGestureRecognizer:tapGester];
    [self.view addSubview:_commentBGView];
    [self.view addSubview:self.sendCommentView];
}


#pragma mark - 初始化播放器

- (void)initVideoPlayer{
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.isPresented = NO;
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    
    // 播放指定 vid 的视频
    [self.videoPlayer setVid:@"873c41fa75775e5642add081643ffb75_8"];
    
//    [self.view addSubview:self.videoPlayer.view];
    [self.videoPlayer setParentViewController:self];
    self.videoPlayer.NOFullSCreen = YES;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.videoPlayer.view.bounds];
    imageView.image = [UIImage imageNamed:@"yinping_bg"];
    imageView.backgroundColor = [UIColor greenColor];
    [self.videoPlayer.view addSubview:imageView];
    [self.videoPlayer.view bringSubviewToFront:self.videoPlayer.videoControl];
    // 需要保留导航栏
    [self.videoPlayer keepNavigationBar:YES];
    [self.videoPlayer setNavigationController:self.navigationController];
    
    // 设置附加组件
    [self.videoPlayer setHeadTitle:self.video.title];
    [self.videoPlayer setEnableDanmuDisplay:NO];      // 不显示弹幕按钮
    [self.videoPlayer setEnableRateDisplay:NO];       // 不显示播放速率按钮
    
    UIImage *logo = [UIImage imageNamed:@"pvlogo.png"];
    [self.videoPlayer setLogo:logo location:PvLogoLocationTopLeft size:CGSizeMake(70, 30) alpha:0.8]; // 设置logo
    
    // 开启片头播放
    // self.videoPlayer.teaserEnable = YES;
    
    // 自动续播, 是否继续上次观看的位置
    self.videoPlayer.autoContinue = YES;
    
    // 开启弹幕
    [self.videoPlayer enableDanmu:NO];
    
    // 是否开启截图
    self.videoPlayer.enableSnapshot = NO;
    
    // 设置是否自动播放，默认为YES
    self.videoPlayer.shouldAutoplay = YES;
    
    /**
     *  ---- 回调代码块 ----
     */
    __weak typeof(self) weakSelf = self;
    [self.videoPlayer setPlayButtonClickBlock:^{
        weakSelf.videoPlayerMeun.startButton.selected = YES;
    }];
    [self.videoPlayer setPauseButtonClickBlock:^{
        weakSelf.videoPlayerMeun.startButton.selected = NO;
    }];
    
    [self.videoPlayer setFullscreenBlock:^{
        //NSLog(@"should hide toolbox in this viewcontroller if needed");
    }];
    [self.videoPlayer setShrinkscreenBlock:^{
        //NSLog(@"show toolbox back if needed");
    }];
    
    // 视频播放完成的回调代码块
    [self.videoPlayer setWatchCompletedBlock:^{
       
    }];
    
}

//关闭右滑pop
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return gestureRecognizer != self.navigationController.interactivePopGestureRecognizer;
}

#pragma mark - 通知
//视频控制器时间的变化
- (void)currentTimeAndTotalTime:(NSNotification *)info{
    
    self.videoPlayerMeun.currentTime.text = info.object[@"currentTime"];
    self.videoPlayerMeun.totalTime.text = info.object[@"totalTime"];
}
- (void)IntroduceCell_reLoadTableViewCell:(NSNotification *)info{
    NSDictionary *dict = info.userInfo;
    NSString *synopsisIntroduce = [dict objectForKey:SynopsisIntroduce];
    NSString *teachIntroduce1 = [dict objectForKey:teachIntroduce];
    
    if ([synopsisIntroduce isEqualToString:@"1"]) {//课程简介展开
        _synopsisIntroduce_isUnfold = YES;
    }else{//课程简介收缩
        _synopsisIntroduce_isUnfold = NO;
    }
    if ([teachIntroduce1 isEqualToString:@"1"]) {//教师简介展开
        _teacherIntroduce_isUnfold = YES;
    }else{//教师简介收缩
        _teacherIntroduce_isUnfold = NO;
    }
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    
    [self ZJStudyCoureSwithViewDelegate_synopsisClick];
}


#pragma mark - 键盘处理

- (void)action_tapGester{
    [self.view endEditing:YES];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    _commentBGView.hidden = NO;
    self.sendCommentView.hidden = NO;
    [self.view bringSubviewToFront:self.sendCommentView];
    
    CGFloat curkeyBoardHeight = [[[notification userInfo] objectForKey:@"UIKeyboardBoundsUserInfoKey"] CGRectValue].size.height;
    
    NSTimeInterval interal = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //计算 视图应该移动的位置
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:interal animations:^{
        
        weakSelf.sendCommentView.frame = CGRectMake(0, [UIScreen zj_screenHeight] - weakSelf.sendCommentView.height - curkeyBoardHeight, [UIScreen zj_screenWith], weakSelf.sendCommentView.height);
    }];
}

- (void)keyboardWillDismiss:(NSNotification *)notification {
    _commentBGView.hidden = YES;
    self.sendCommentView.hidden = YES;
    NSTimeInterval interal = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    //计算 视图应该移动的位置
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:interal animations:^{
        weakSelf.sendCommentView.frame = CGRectMake(0, [UIScreen zj_screenHeight], [UIScreen zj_screenWith], weakSelf.sendCommentView.height);
        
    }];
}




#pragma mark - 数据源获取
//获取课程简介
- (void)networking_introduce{
    [self.dataArray removeAllObjects];
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"COURSEWARE_ID":self.model.ID};
    [NetWorkingManager postWithUrlString:[NetWorkingURL courseware] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//
            ZJSCSynopsisModel *model = [ZJSCSynopsisModel yy_modelWithJSON:object];
            [weakSelf.dataArray addObject:model];
            //            [weakSelf.tableView reloadData];
            [weakSelf networking_comment];
        }
    } Fail:^(id object) {
        
    }];
    
}
/** 获取评论 */
- (void)networking_comment{
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"STUDENTID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,
                           @"COURSEWAREID":self.model.ID,
                           @"PAGE_NUMBER":@"1",
                           @"SHOW_NUMBER":@"10"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL listClassFAQ] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000" ]) {//获取成功
            NSMutableArray *array = [[NSArray yy_modelArrayWithClass:[ZJSCQusetionsSrectionModel class] json:object[@"CLASS_FAQ"]] mutableCopy];
            
            if (array.count > 0) {//有数据
                
                if (array.count < 5) {//显示五条数据
                    [weakSelf.dataArray addObject:array];
                }else{
                    NSMutableArray *newArray = [[array subarrayWithRange:NSMakeRange(0, 5)] mutableCopy];
                    [newArray addObject:@{@"name":@"点击查看更多评论",@"type":@"1"}];
                    [weakSelf.dataArray addObject:newArray];
                    
                }
            }else{//没有数据
                [weakSelf.dataArray addObject:@[@{@"imageName":@"zanwupinglun",@"type":@"0"}]];
            }
            //            [weakSelf.tableView reloadData];
            [weakSelf networking_note];
        }
    } Fail:^(id object) {
        
    }];
    
}
/** 获取笔记 */
- (void)networking_note{
    
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"USER_ID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,
                           @"COURSEWARE_ID":self.model.ID,
                           @"PAGE_NUMBER":@"1",
                           @"SHOW_NUMBER":@"10"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL listCourseNote] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//
            NSMutableArray *array = [[NSArray yy_modelArrayWithClass:[ZJSCNoteModel class] json:object[@"NOTES"]] mutableCopy];
            if (array.count > 0) {//有数据
                
                if (array.count < 5) {//显示五条数据
                    [weakSelf.dataArray addObject:array];
                }else{
                    NSMutableArray *newArray = [[array subarrayWithRange:NSMakeRange(0, 5)] mutableCopy];
                    [newArray addObject:@{@"name":@"点击查看更多笔记",@"type":@"1"}];
                    [weakSelf.dataArray addObject:newArray];
                    
                }
            }else{
                [weakSelf.dataArray addObject:@[@{@"imageName":@"zanwubiji",@"type":@"0"}]];
            }
            
            
            [weakSelf.tableView reloadData];
        }
    } Fail:^(id object) {
        
    }];
}

#pragma mark - ZJSCNoteTableViewCellDelegate（笔记cell） 协议方法
/** 删除笔记 */
- (void)ZJSCNoteTableViewCellDelegate_DeleteButton:(ZJSCNoteModel *)model{
    [self networking_introduce];
}
#pragma mark - ZJStudyCourseNoteTableViewCellDelegate(笔记) 协议方法
/** 查看更多笔记 */
- (void)ZJStudyCourseNoteTableViewCellDelegate_clickMoreNote{
    __weak typeof(self) weakSelf = self;
    StudyCourseNoteViewController *vc = [StudyCourseNoteViewController new];
    vc.model = self.model;
    vc.currTime = self.videoPlayer.currentTime;
    vc.block = ^(){//刷新tableview
        [weakSelf networking_introduce];
    };
    
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - ZJSCQusetionsSrectionViewCommentDelegate（评论SectionView） 协议方法

//点击评论
- (void)ZJSCQusetionsSrectionViewDelegate_CommentClickToModel:(ZJSCQusetionsSrectionModel *)model{
    _isCommentORNote = YES;
    _commentSectionViewModel = model;
    _isCourseComment = NO;
    [self.sendCommentView.commentTextView.textView becomeFirstResponder];
}

#pragma mark - ZJStudyCourseQuestionsTableViewCellDelegate（评论） 协议方法
/** 查看等多的评论 */
- (void)ZJStudyCourseQuestionsTableViewCellDelegate_clickMoreComment{
    __weak typeof(self) weakSelf = self;
    StudyCourseQuestionsViewController *vc = [StudyCourseQuestionsViewController new];
    vc.model = self.model;
    vc.currTime = self.videoPlayer.currentTime;
    vc.block = ^(){//刷新tableview
        [weakSelf networking_introduce];
    };
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - ZJSCQusetionsTableViewCellDelegate （评论cell）协议方法
//Cell上的按钮被点击
- (void)ZJSCQusetionsTableViewCellDelegate_operateSuccessToModel:(ZJSCQusetionsCellModel *)model indexPatch:(NSIndexPath *)indexPatch{
    ZJSCQusetionsSrectionModel *sectionModel = self.dataArray[indexPatch.section];
    sectionModel.childModelArray[indexPatch.row] = model;
    
    self.dataArray[indexPatch.section] = sectionModel;
}

#pragma mark - ZJStudyCoureSwithViewDelegate（选择） 协议方法
- (void)ZJStudyCoureSwithViewDelegate_synopsisClick{
    CGFloat videoH = self.videoPlayer.view.height ;
 
    [self.tableView setContentOffset:CGPointMake(0, videoH) animated:YES];
}
- (void)ZJStudyCoureSwithViewDelegate_commentClick{
    
    CGFloat videoH = self.videoPlayer.view.height ;
    CGFloat cellHegith = [[cellHeightDict objectForKey:@"0"] floatValue] + videoH;
   
    [self.tableView setContentOffset:CGPointMake(0, cellHegith) animated:YES];
}
- (void)ZJStudyCoureSwithViewDelegate_noteClick{
    CGFloat videoH = self.videoPlayer.view.height ;
    CGFloat cellHegith = [[cellHeightDict objectForKey:@"0"] floatValue] + videoH;
    CGFloat cellHegith1= [[cellHeightDict objectForKey:@"1"] floatValue];
    CGFloat cellHegith2= [[cellHeightDict objectForKey:@"2"] floatValue];
    
    CGFloat offsetY = cellHegith + cellHegith1 + cellHegith2 > self.tableView.contentSize.height - self.tableView.height ? self.tableView.contentSize.height - self.tableView.height : cellHegith + cellHegith1;
    [self.tableView setContentOffset:CGPointMake(0, offsetY) animated:YES];
    
}
#pragma mark - ZJVideoControllerMeunViewDelegate(播放控制器) 协议方法

- (void)ZJVideoControllerMeunViewDelegate_startButton:(UIButton *)sender{
    if (sender.selected) {
        [self.videoPlayer playButtonClick];
    }else{
        [self.videoPlayer pauseButtonClick];
    }
}

- (void)ZJVideoControllerMeunViewDelegate_backward:(UIButton *)sender{
    self.videoPlayer.videoControl.slider.progressValue -= 30.0f;
    [self.videoPlayer setCurrentPlaybackTime:floor(self.videoPlayer.videoControl.slider.progressValue)];
}

- (void)ZJVideoControllerMeunViewDelegate_speed:(UIButton *)sender{
    CGFloat max_Time = self.videoPlayer.videoControl.slider.progressValue + 30.0f;
    
    NSMutableDictionary *maxTimeDict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:USER_DICT_MAX_PLAYTIME].mutableCopy;
    
    if (!maxTimeDict){
        self.videoPlayer.videoControl.slider.progressValue = 0;
        
    }else{
        
        double userDefaultsTime = [maxTimeDict[self.videoPlayer.vid] doubleValue];
        if (max_Time >= userDefaultsTime) {
            [YJProgressHUD showMsgWithoutView:MAXTIME_HINT];
            self.videoPlayer.videoControl.slider.progressValue = userDefaultsTime;
        }else{
            self.videoPlayer.videoControl.slider.progressValue = max_Time;
        }
        
        [self.videoPlayer setCurrentPlaybackTime:floor(self.videoPlayer.videoControl.slider.progressValue)];
    }
}

#pragma mark - ZJStudyCoureFooterViewDelegate（下部footerView） 协议方法
/** 评论输入框 */
- (void)ZJStudyCoureFooterViewDelegate_commentTextButtonClick{
    _isCommentORNote = YES;
    _isCourseComment = YES;
    self.sendCommentView.hidden = NO;
    [self.sendCommentView.commentTextView.textView becomeFirstResponder];
}
/** 评论按钮 */
- (void)ZJStudyCoureFooterViewDelegate_commentButtonClick{//跳评论页面
    __weak typeof(self) weakSelf = self;
    StudyCourseQuestionsViewController *vc = [StudyCourseQuestionsViewController new];
    vc.model = self.model;
    vc.currTime = self.videoPlayer.currentTime;
    vc.block = ^(){//刷新tableview
        [weakSelf networking_introduce];
    };
    [self.navigationController pushViewController:vc animated:YES];
    
}
/** 笔记按钮 */
- (void)ZJStudyCoureFooterViewDelegate_noteButtonClick{
    _isCommentORNote = NO;
    self.sendCommentView.hidden = NO;
    [self.sendCommentView.commentTextView.textView becomeFirstResponder];
}
/** 收藏按钮 */
- (void)ZJStudyCoureFooterViewDelegate_collectButtonClick{
    
}
#pragma mark - SendCommentViewDelegate(发送评论) 协议方法
//发送评论
- (void)sendButtonClick{
    [self.view endEditing:YES];
    NSDictionary *dict = @{};
    if (_isCommentORNote) {//评论
        if (_isCourseComment) {//是课程评论
            dict = @{@"USERID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,//当前用户id
                     @"COURSEWAREID":self.model.ID,//课程id
                     @"ACCOUNTID":[ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.ID,//账户id
                     @"CONTENT":self.sendCommentView.commentTextView.textView.text,//评论内容
                     @"VIDEOSTAMP":@(self.videoPlayer.currentTime),
                     @"UNITID":[ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.CURRENTIDENTITY //机构id
                     };
        }else{//评论-评论
            dict = @{@"USERID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,//当前用户id
                     @"COURSEWAREID":self.model.ID,//课程id
                     @"ACCOUNTID":[ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.ID,//账户id
                     @"CONTENT":self.sendCommentView.commentTextView.textView.text,//评论内容
                     @"VIDEOSTAMP":@(self.videoPlayer.currentTime),
                     @"UNITID":[ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.CURRENTIDENTITY, //机构id
                     @"FID":_commentSectionViewModel.ID
                     };
        }
        [self netWorkingCommentWithDict:dict];
        
    }else{//笔记
        
        [self netWorkingNote];
    }
}
//发送评论的网络请求
//评论
- (void)netWorkingCommentWithDict:(NSDictionary *)dict{
    [YJProgressHUD show:@"" inView:self.view mode:YJProgressModeLoading];
    __weak typeof(self) weakSelf = self;
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL addClassFAQ] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//评论成功
            [YJProgressHUD showMsgWithoutView:@"评论成功"];
            [weakSelf networking_introduce];
        }else{
            
            [YJProgressHUD showMsgWithoutView:@"评论失败"];
        }
    } Fail:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"评论失败"];
    }];
    
}
//写笔记的网络请求

//笔记
- (void)netWorkingNote{
    [YJProgressHUD show:@"" inView:self.view mode:YJProgressModeLoading];
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"USER_ID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,//用户id
                           @"VIDEO_STAMP":@(self.videoPlayer.currentTime),//视频时间
                           @"COURSEWARE_ID":self.model.ID,//课程id
                           @"UNITID":[ZJUserInfoModel sharedUserInfo].DEPT_MODEL_JOB.ID,//机构id
                           @"NOTE_CONTENT":self.sendCommentView.commentTextView.textView.text};//笔记内容
    [NetWorkingManager postWithUrlString:[NetWorkingURL courseNote] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        [YJProgressHUD hide];
        if ([model.STATE isEqualToString:@"20000"]) {//记笔记成功
            [weakSelf networking_introduce];
        }else{
            [YJProgressHUD showMsgWithoutView:@"记笔记失败"];
        }
    } Fail:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"记笔记失败"];
    }];
    
}

#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {//课程简介
        ZJStudyCourseSynopsisTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SYNOPSISSCECTIONVIEW forIndexPath:indexPath];
        cell.model.synopsisIntroduce_isUnfold = _synopsisIntroduce_isUnfold;
        cell.model.teacherIntroduce_isUnfold = _teacherIntroduce_isUnfold;
        cell.model = self.dataArray[indexPath.row];
        return cell;
    }else if (indexPath.row == 1){//评论
        ZJStudyCourseQuestionsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:COMMENTCELL forIndexPath:indexPath];
        cell.array = self.dataArray[indexPath.row];
        cell.delegate = self;
        return cell;
        
    }else{//笔记
        
        ZJStudyCourseNoteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NOTECELL forIndexPath:indexPath];
        cell.dataArray = [self.dataArray[indexPath.row] mutableCopy];
        cell.delegate = self;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {//课程简介
        ZJStudyCourseSynopsisTableViewCellModel *model = [ZJStudyCourseSynopsisTableViewCellModel new];
        model.model = self.dataArray[indexPath.row];
        model.model.synopsisIntroduce_isUnfold = _synopsisIntroduce_isUnfold;
        model.model.teacherIntroduce_isUnfold = _teacherIntroduce_isUnfold;
        [cellHeightDict setValue:@(model.cellHeight) forKey:[NSString stringWithFormat:@"%ld",indexPath.row]];
        
        return model.cellHeight;
    }else if (indexPath.row == 1){//评论
        ZJStudyCourseQuestionsTableViewCellModel * model= [ZJStudyCourseQuestionsTableViewCellModel new];
        model.array = self.dataArray[indexPath.row];
        
        [cellHeightDict setValue:@(model.cellHeight) forKey:[NSString stringWithFormat:@"%ld",indexPath.row]];
        return model.cellHeight;
    }else{//笔记
        
        ZJStudyCourseNoteTableViewCellModel * model= [ZJStudyCourseNoteTableViewCellModel new];
        model.array = self.dataArray[indexPath.row];
        [cellHeightDict setValue:@(model.cellHeight) forKey:[NSString stringWithFormat:@"%ld",indexPath.row]];
        return model.cellHeight;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat videoH = self.videoPlayer.view.height ;
    
    CGFloat cellHegith = [[cellHeightDict objectForKey:@"0"] floatValue] + videoH;
    CGFloat cellHegith1= [[cellHeightDict objectForKey:@"1"] floatValue];
    CGFloat cellHegith2 = [[cellHeightDict objectForKey:@"2"] floatValue];
    
    
    if (scrollView.contentOffset.y > 0 && scrollView.contentOffset.y < cellHegith) {
        [self.swithView selectNumberString:@"1"];
    }else if (scrollView.contentOffset.y >= cellHegith && scrollView.contentOffset.y < (cellHegith + cellHegith1)) {
        [self.swithView selectNumberString:@"2"];
    }else if (scrollView.contentOffset.y >= (cellHegith + cellHegith1) && scrollView.contentOffset.y < (cellHegith + cellHegith1 + cellHegith2)) {
        [self.swithView selectNumberString:@"3"];
    }
    if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.height)) {
        [self.swithView selectNumberString:@"3"];
    }

    
    CGPoint offPoint = scrollView.contentOffset;
    if (offPoint.y > self.videoPlayer.view.height) {
        if ([self.videoPlayerMeun superview] == self.view) {
            return;
        }
        self.videoPlayerMeun.frame = CGRectMake(0, navHeight, self.view.width, self.videoPlayerMeun.height);
        self.swithView.frame = CGRectMake(0, self.videoPlayerMeun.bottom, self.view.width, self.swithView.height);
        [self.view addSubview:self.videoPlayerMeun];
        [self.view addSubview:self.swithView];
    }else{
        if ([self.videoPlayerMeun superview] == self.headerView) {
            return;
        }
        self.videoPlayerMeun.frame = CGRectMake(0, self.videoPlayer.view.bottom, self.view.width, self.videoPlayerMeun.height);
        self.swithView.frame = CGRectMake(0, self.videoPlayerMeun.bottom, self.view.width, self.swithView.height);
        [self.headerView addSubview:self.videoPlayerMeun];
        [self.headerView addSubview:self.swithView];
        [self.headerView bringSubviewToFront:self.videoPlayer.view];
    }

    
}


#pragma mark - Setter
#pragma mark - Getters

- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, navHeight, self.view.width, self.footerView.y - navHeight)  style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headerView;
        
        [_tableView  registerClass:[ZJStudyCourseNoteTableViewCell class] forCellReuseIdentifier: NOTECELL];
        [_tableView  registerClass:[ZJStudyCourseSynopsisTableViewCell class] forCellReuseIdentifier:SYNOPSISSCECTIONVIEW];
        [_tableView  registerClass:[ZJStudyCourseQuestionsTableViewCell class] forCellReuseIdentifier: COMMENTCELL];
        
    }
    return _tableView;
}





- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (ZJStudyCoureFooterView *)footerView{
    if (_footerView == nil){
        _footerView = [[ZJStudyCoureFooterView alloc]initWithFrame:CGRectMake(0, self.view.height - TabbarHeight, self.view.width, TabbarHeight)];
        _footerView.delegate = self;
    }
    return _footerView;
}

- (SkinVideoViewController *)videoPlayer{
    if (!_videoPlayer) {
        CGFloat width = self.view.bounds.size.width;
        _videoPlayer = [[SkinVideoViewController alloc] initWithFrame:CGRectMake(0, 0, width, width * (9.0/16.0))];
        [_videoPlayer configObserver];
    
    }
    return _videoPlayer;
}

- (ZJVideoControllerMeunView *)videoPlayerMeun{
    if (_videoPlayerMeun == nil){
        CGFloat width = self.view.bounds.size.width;
        _videoPlayerMeun = [[ZJVideoControllerMeunView alloc] initWithFrame:CGRectMake(0, width*(9.0/16.0) , self.view.width, 44)];
        _videoPlayerMeun.backgroundColor = [UIColor whiteColor];
        _videoPlayerMeun.delegate = self;
        _videoPlayerMeun.startButton.selected = NO;
        if (!self.isCandragging) {
            _videoPlayerMeun.backward.userInteractionEnabled = NO;
            _videoPlayerMeun.speed.userInteractionEnabled = NO;
        }
    }
    return _videoPlayerMeun;
}
- (ZJStudyCoureSwithView *)swithView{
    if (_swithView == nil){
        _swithView = [[ZJStudyCoureSwithView alloc] initWithFrame:CGRectMake(0, self.videoPlayerMeun.bottom, self.view.width, 44 * [UIScreen zj_screenHScale])];
        _swithView.delegate = self;
        _swithView.backgroundColor = [UIColor whiteColor];
    }
    return _swithView;
}

- (UIView *)headerView{
    if (_headerView == nil){
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.swithView.bottom)];
        [_headerView addSubview:self.videoPlayer.view];
        [_headerView addSubview:self.videoPlayerMeun];
        [_headerView addSubview:self.swithView];
        
        [_headerView bringSubviewToFront:self.videoPlayer.view];

    }
    return _headerView;
}
-(SendCommentView *)sendCommentView{
    if (_sendCommentView == nil){
        _sendCommentView = [[SendCommentView alloc]initWithFrame:CGRectMake(0, [UIScreen zj_screenHeight], [UIScreen zj_screenWith], [UIScreen zj_screenHeight] * 0.2)];
        _sendCommentView.commentDelegate = self;
    }
    return _sendCommentView;
}

@end
